package com.example.administrator.kotiln.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.base.BaseActivity;
import com.example.administrator.kotiln.util.ToastUtil;
import com.example.administrator.kotiln.utils.Share;
import com.example.administrator.kotiln.utils.ToastUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * Created by Administrator on 2017/11/8.
 */


public class OkHttpWebSocketActivity extends BaseActivity {

    private String WEBSOCKETPATH = "ws://192.168.1.240:9505/";

    public WebSocket socket = null;
    private OkHttpClient client;
    private EditText tv;
    private String clientmessage;
    private String text1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttpwebsocket);
        initValue();
    }

    private void initValue() {

        tv = (EditText) findViewById(R.id.tv);

        client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(8, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder().url(WEBSOCKETPATH).build();

        client.newWebSocket(request, new WebSocketListener() {
            //创建线程池，
            ExecutorService writeExecutor = Executors.newSingleThreadExecutor();
            WebSocket webSocket = null;


            @Override
            public void onOpen(final WebSocket webSocket, Response response) {
                super.onOpen(webSocket, response);
                this.webSocket = webSocket;
                writeExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        //  webSocket.send("请求");
                    }
                });

                handler.sendEmptyMessage(0);
            }

            @Override
            public void onMessage(WebSocket webSocket, String text) {
                super.onMessage(webSocket, text);
                Share.d("onMessage" + text);
                text1 = text;
                handler.sendEmptyMessage(1);


            }

            @Override
            public void onMessage(WebSocket webSocket, ByteString bytes) {
                super.onMessage(webSocket, bytes);
                Share.d("onMessagen//" + bytes);
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onClosing(WebSocket webSocket, int code, String reason) {
                super.onClosing(webSocket, code, reason);
                Share.d("onClosing" + reason);
                clientmessage = reason;
                handler.sendEmptyMessage(3);
            }

            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                super.onClosed(webSocket, code, reason);
                Share.d("onClosed" + reason);
                handler.sendEmptyMessage(4);
            }

            @Override
            public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
                super.onFailure(webSocket, t, response);
                Share.d("onFailure" + response);
                handler.sendEmptyMessage(5);
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    ToastUtil.show(OkHttpWebSocketActivity.this, "打开onOpen");
                    break;
                case 1:
                    ToastUtils.showToast(OkHttpWebSocketActivity.this, "有数据");
                    tv.setText(text1);
                    text1 = "";
                    break;
                case 2:
                    ToastUtil.show(OkHttpWebSocketActivity.this, "链接失败onMessage////");
                    break;
                case 3:
                    ToastUtil.show(OkHttpWebSocketActivity.this, "链接失败onClosing");
                    break;
                case 4:
                    ToastUtil.show(OkHttpWebSocketActivity.this, "链接失败onClosed");
                    break;
                case 5:
                    ToastUtil.show(OkHttpWebSocketActivity.this, "链接失败onFailure");
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //socket.cancel();
    }
}
